/*============================================================================*/
/*                   CONTINENTAL AUTOMOTIVE SYSTEMS                           */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* name:            MALbeMIOS.h
* version:         1.1
* created_by:      Rodrigo Casados
* date_created:    June 7 2013
*=============================================================================*/
/* DESCRIPTION : Source file to implement main function.                      */
/*============================================================================*/
/* FUNCTION COMMENT : Implements entry point (main function) and other        */
/* application stuff.                                                         */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*  1.1      | 7/June/2013 |                               | Rodrigo Casados */
/* Initial version for the Smart Car Race 2010 contest using Freescale        */
/* Bolero micro.                                                              */
/*============================================================================*/

/* Includes */
/* -------- */
#include "stdtypedef.h"

#include "MALbeMIOS.h"

void MALbeMIOS0_PITch0_installISR(void);
/* Exported functions */
extern void MALbeMIOS0_PITch0_ISR(void);
/* ------------------ */

/**************************************************************
 *  Name                 : MALbeMIOS_initEMIOS0
 *  Description          : Initialize eMIOS0.
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
void MALbeMIOS_initEMIOS0(void)
{
	/* MCR register */
	/* Ensure that eMIOS0 is not disabled */
	EMIOS_0.MCR.B.MDIS = 0; 
	/* Set eMIOS0 prescaler (clock division is equal to the GPRE value + 1) */
	EMIOS_0.MCR.B.GPRE = 63; /* 63 means a clock division of 64. If System clock is 64 MHz, eMIOS0 clock will be 1 MHz */ 
	/* Enable prescaler for eMIOS0 */
	EMIOS_0.MCR.B.GPREN = TRUE;
	/* */ 
	EMIOS_0.MCR.B.GTBE = TRUE;
	
	EMIOS_0.MCR.B.FRZ = 1;
}

/**************************************************************
 *  Name                 : MALbeMIOS_initEMIOS0_ch23
 *  Description          : Initialize eMIOS0 channel 23.
 *  Parameters           :  none.
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
void MALbeMIOS_initEMIOS0_ch23(void)
{
	/* Ensure that Channel is Enabled */
	EMIOS_0.UCDIS.B.CHDIS23 = FALSE;
	
	/* Configure Channel 23 Paramenters */
	/* -------------------------------- */ 
		/* Set Registers A and B for this channel */
	EMIOS_0.CH[23].CADR.R = 0x2710;//This value indicates the value to make the change of the rotatiion(Duty cycle)
         EMIOS_0.CH[23].CBDR.R = 0x4E20;//This Register indicates the Value for period
  								   /* Register B is set at its maximum value in order to 
  	                               /* control PWM duty cycle just with register A */                             	
	/* Set eMIOS0 Channel 23 prescaler (clock division is equal to the UCPRE value + 1) */
	EMIOS_0.CH[23].CCR.B.UCPRE = 0;	/* Clock prescaler of 1 for this channel */
	/* Enable clock prescaler for this channel */
	EMIOS_0.CH[23].CCR.B.UCPEN = TRUE;
	/* Select Internal Counter */
	EMIOS_0.CH[23].CCR.B.BSL = 0x0; 
	/* Select Channel mode */
 	EMIOS_0.CH[23].CCR.B.MODE=0x58;	//Configure as Output Pulse Width and Frequency Modulation Buffered
}
/**************************************************************
 *  Name                 : MALbeMIOS_initEMIOS0_ch6
 *  Description          : Initialize eMIOS0 channel 6.
 *  Parameters           :  none.
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
void MALbeMIOS_initEMIOS0_ch6(void)
{
	/* Ensure that Channel is Enabled */
	EMIOS_0.UCDIS.B.CHDIS6 = FALSE;
	
	/* Configure Channel 6 Paramenters */
	/* -------------------------------- */ 
		/* Set Registers A and B for this channel */
	EMIOS_0.CH[6].CADR.R = 0x2710;//This value indicates the value to make the change of the rotatiion(Duty cycle)
         EMIOS_0.CH[6].CBDR.R = 0x4E20;//This Register indicates the Value for period
  								   /* Register B is set at its maximum value in order to 
  	                               /* control PWM duty cycle just with register A */                             	
	/* Set eMIOS0 Channel 23 prescaler (clock division is equal to the UCPRE value + 1) */
	EMIOS_0.CH[6].CCR.B.UCPRE = 0;	/* Clock prescaler of 1 for this channel */
	/* Enable clock prescaler for this channel */
	EMIOS_0.CH[6].CCR.B.UCPEN = TRUE;
	/* Select Internal Counter */
	EMIOS_0.CH[6].CCR.B.BSL = 0x0; 
	/* Select Channel mode */
 	EMIOS_0.CH[6].CCR.B.MODE=0x58;	//Configure as Output Pulse Width and Frequency Modulation Buffered
}
/**************************************************************
 *  Name                 : MALbeMIOS_initEMIOS0_ch7
 *  Description          : Initialize eMIOS0 channel 7.
 *  Parameters           :  none.
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
void MALbeMIOS_initEMIOS0_ch7(void)
{
	/* Ensure that Channel is Enabled */
	EMIOS_0.UCDIS.B.CHDIS7 = FALSE;
	
	/* Configure Channel 7 Paramenters */
	/* -------------------------------- */ 
		/* Set Registers A and B for this channel */
	EMIOS_0.CH[7].CADR.R = 0x4E20;//This value indicates the value to make the change of the rotatiion(Duty cycle)
         EMIOS_0.CH[7].CBDR.R = 0x4E20;//This Register indicates the Value for period
  								   /* Register B is set at its maximum value in order to 
  	                               				/* control PWM duty cycle just with register A */                             	
	/* Set eMIOS0 Channel 23 prescaler (clock division is equal to the UCPRE value + 1) */
	EMIOS_0.CH[7].CCR.B.UCPRE = 0;	/* Clock prescaler of 1 for this channel */
	/* Enable clock prescaler for this channel */
	EMIOS_0.CH[7].CCR.B.UCPEN = TRUE;
	/* Select Internal Counter */
	EMIOS_0.CH[7].CCR.B.BSL = 0x0; 
	/* Select Channel mode */
 	EMIOS_0.CH[7].CCR.B.MODE=0x58;	//Configure as Output Pulse Width and Frequency Modulation Buffered
}
